package com.runvp.common.util.enc;

import java.nio.charset.StandardCharsets;

public class HexStringToASCII {
    public static String convert(String hexString) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < hexString.length() - 1; i += 2) {
            String hex = hexString.substring(i, i + 2);
            int decimal = Integer.parseInt(hex, 16);
            sb.append((char) decimal);
        }
        return sb.toString();
    }


    public static String hexToAscii(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0, j = 0; i < hexString.length(); i += 2, j++) {
            String hex = hexString.substring(i, i + 2);
            bytes[j] = (byte) Integer.parseInt(hex, 16);
        }
        return new String(bytes, StandardCharsets.UTF_8);
    }
    public static  byte[] hexToAsciiByte(String hexString) {
        byte[] bytes = new byte[hexString.length() / 2];
        for (int i = 0, j = 0; i < hexString.length(); i += 2, j++) {
            String hex = hexString.substring(i, i + 2);
            bytes[j] = (byte) Integer.parseInt(hex, 16);
        }
        return bytes;
    }

    public static String hex_to_ascii(String hex_str) {
        hex_str = hex_str.replace(" ", "").replace("0x", "").replace("\\x", "");
        StringBuilder ascii_str = new StringBuilder();
        for (int i = 0; i < hex_str.length(); i += 2) {
            String str = hex_str.substring(i, i + 2);
            ascii_str.append((char) Integer.parseInt(str, 16));
        }
        return ascii_str.toString();
    }

    public static void main(String[] args) {
        System.out.println(convert("6BE3153AFC79025ACDF03CB38D988927BBF1DB4FCE387729CB19AB13FEC136"));
        System.out.println(hex_to_ascii("6BE3153AFC79025ACDF03CB38D988927BBF1DB4FCE387729CB19AB13FEC136"));
    }
}